package com.itbaizhan.controller;

import com.itbaizhan.entity.MembersInfo;
import com.itbaizhan.service.MembersInfoService;
import com.itbaizhan.util.AjaxResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luo20
 */
@RestController
@RequestMapping("/membersInfo")
public class MembersInfoController {

    @Autowired
    private MembersInfoService membersInfoService;

    // 查询所有会员信息
    @GetMapping("/findAll")
    public AjaxResult findAll() {
        List<MembersInfo> membersInfoList = membersInfoService.findAll();
        if (!CollectionUtils.isEmpty(membersInfoList)){
            return AjaxResult.success("查询成功",membersInfoList);
        }else {
            return AjaxResult.error("查询失败");
        }
    }

    // 根据id删除会员信息
    @DeleteMapping("{id}")
    public AjaxResult deleteById(@PathVariable("id") Integer id) {
        int i = membersInfoService.deleteById(id);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }
    //跟据id查询信息
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable("id") Integer id) {
        MembersInfo membersInfo = membersInfoService.findById(id);
        if (!ObjectUtils.isEmpty(membersInfo)){
            return AjaxResult.success("查询成功",membersInfo);
        }else {
            return AjaxResult.error("查询失败");
        }
    }
    //根据id修改信息1
    // @PutMapping("/updateById")
    // public AjaxResult updateById(@RequestBody MembersInfo membersInfo) {
    //     int i = membersInfoService.updateById(membersInfo);
    //     if (i > 0){
    //         return AjaxResult.success("修改成功");
    //     }else {
    //         return AjaxResult.error("修改失败");
    //     }
    // }

    //根据id修改信息2
    @PutMapping("/updateById")
    public AjaxResult updateById(@ModelAttribute MembersInfo membersInfo) {
        if (membersInfo.getMgender().equals("1")){
            membersInfo.setMgender("男");
        }else {
            membersInfo.setMgender("女");
        }
        int i = membersInfoService.updateById(membersInfo);
        if (i > 0){
            return AjaxResult.success("更新会员信息成功");
        }else {
            return AjaxResult.error("更新会员信息失败");
        }
    }



}
